package whf.framework.workflow.engine;

import java.io.InputStream;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.graph.exe.Token;
import org.jbpm.taskmgmt.exe.TaskInstance;

import whf.framework.workflow.entity.WorkflowEntity;
import whf.framework.workflow.exception.WorkflowException;

/**
 * @author King
 * 工作流引擎
 */
public interface WorkflowEngine {
	public abstract JbpmConfiguration getConfiguration();
	
	public abstract JbpmContext getContext();

	public abstract void createSchema();

	public abstract void dropSchema();
	/**
	 * 搜索流程定义
	 * @param id
	 * @return
	 * @throws WorkflowException
	 */
	public abstract ProcessDefinition getProcessDefinition(long id) throws WorkflowException;

	public abstract ProcessDefinition getProcessDefinitionByName(String name) throws WorkflowException;
	
	public abstract boolean isDefinitionDeployed(long processDefinitionId) throws WorkflowException;

	public abstract ProcessDefinition deployDefinition(InputStream stream) throws WorkflowException;

	public abstract void undeployDefinition(ProcessDefinition processDefinition) throws WorkflowException;
	
	/**
	 * 启动流程实例
	 * @param processDefinition
	 * @param entity
	 * @param attrs
	 * @return
	 * @throws WorkflowException
	 */
	public abstract ProcessInstance startProcessInstance(ProcessDefinition processDefinition, WorkflowEntity entity, Map<String, Object> attrs) throws WorkflowException;

	/**
	 * 终止流程实例
	 * @param processInstance
	 * @return
	 * @throws WorkflowException
	 */
	public abstract ProcessInstance terminateProcessInstance(ProcessInstance processInstance) throws WorkflowException;

	public abstract ProcessInstance getProcessInstance(long id) throws WorkflowException;
	/**
	 * 搜索流程实例
	 * @param actorId
	 * @return
	 * @throws WorkflowException
	 */
	public abstract List<ProcessInstance> findProcessInstancesByActor(String actorId) throws WorkflowException;

	public abstract List<ProcessInstance> findProcessInstancesByProcessDefinition(ProcessDefinition processDefinition) throws WorkflowException;

	public abstract TaskInstance getTaskInstance(long id) throws WorkflowException;
	/**
	 * @param taskInstance
	 * @return
	 */
	public abstract TaskInstance startTaskInstance(TaskInstance taskInstance) throws WorkflowException;

	public abstract TaskInstance suspendTaskInstance(TaskInstance taskInstance) throws WorkflowException;

	public abstract TaskInstance endTaskInstance(TaskInstance taskInstance, String transitionName) throws WorkflowException;

	public abstract TaskInstance updateTaskInstance(TaskInstance taskInstance, Map<String, String> props) throws WorkflowException;

	public abstract void assignTaskInstance(TaskInstance taskInstance, String participant);

	public abstract void unAssignTaskInstance(TaskInstance taskInstance, String participant);
	
	/**
	 * 搜索Task实例
	 * @param username
	 * @return
	 * @throws WorkflowException
	 */
	public abstract Collection<TaskInstance> findTaskInstancesByActor(String actorId) throws WorkflowException;
	
	public abstract Collection<TaskInstance> findTaskInstancesByActorAndProcessNode(String actorId, String processName, String nodeName) throws WorkflowException;
	
	public abstract Collection<TaskInstance> findTaskInstancesByProcessInstance(ProcessInstance processInstance) throws WorkflowException;
	
	public abstract Token followTransition(Token curToken, String transitionName, Map<String, Object> attrs) throws WorkflowException;

}